﻿using System;
using System.Data;
using System.Drawing;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Baimasoft.FrameWork.Base;
using Baimasoft.Manage.SqlServerDAL;

public partial class SysManage_Role_User_List : System.Web.UI.Page
{
    BaiMaSoft.SysManage.UserManage MYUM = new BaiMaSoft.SysManage.UserManage();

    RoleManage MyRm = new RoleManage();
    UserManage MyUm = new UserManage();
    BaimasoftFile MyFile = new BaimasoftFile();
    public string sSLogonID = "", sSRoleName = "", UserID = "", sCodeCol = "", sThisCode = "", UserRole = "";
    public int AllCount = 0, AllPageCount = 0, NowPageCount = 0, PageSize = 0;
    DataTable MyDt;

    protected void Page_Load(object sender, EventArgs e)
    {
        if ((string)Session["Baimasoft_User_LogonID"] == null)
        {
            Response.Redirect("../ReLogon.aspx");
        }
                
        if (!Page.IsPostBack)
        {
            if (ViewState["VLogonID"] == null || ViewState["VLogonID"].ToString().CompareTo("") == 0)
            {
                ViewState["VLogonID"] = "";
            }
            if (ViewState["VPersName"] == null || ViewState["VPersName"].ToString().CompareTo("") == 0)
            {
                ViewState["VPersName"] = "";
            }

            if (ViewState["OpType"] == null)
            {
                ViewState["OpType"] = "1";
            }

            if (ViewState["SortExpression"] == null || ViewState["SortExpression"].ToString().CompareTo("") == 0)
            {
                ViewState["SortExpression"] = "LogonID";
            }

            if (ViewState["SortDirection"] == null || ViewState["SortDirection"].ToString().CompareTo("") == 0)
            {
                ViewState["SortDirection"] = " ";
            }
            string sSqlSort = " ORDER BY " + ViewState["SortExpression"] + " " + ViewState["SortDirection"];
            Bind(sSqlSort);
        }
    }

    private void Bind(string SqlSort)
    {
        if (Session["RoleUser_RoleCode"].ToString().Trim() != "" || Session["RoleUser_RoleCode"].ToString().Trim() != null)
        {
            if (ViewState["OpType"].ToString() == "0")
            {
                
            }
            else if (ViewState["OpType"].ToString() == "1")  //查询状态
            {
                MyDt = MyRm.SearchRoleUserInfo(ViewState["VLogonID"].ToString().Trim(), ViewState["VPersName"].ToString().Trim(), Session["RoleUser_RoleCode"].ToString().Trim(), SqlSort);
            }
            else if (ViewState["OpType"].ToString() == "2") // 显示删除
            {

            }
            GridView1.DataSource = MyDt;
            GridView1.DataBind();
            AllPageCount = GridView1.PageCount;
            NowPageCount = GridView1.PageIndex + 1;
            AllCount = MyDt.Rows .Count;
        }
    }
    protected void SearchUserInfo_ServerClick(object sender, EventArgs e)
    {
        ViewState["OpType"] = "1";
        ViewState["VLogonID"] = LogonID.Text.ToString().Trim();
        ViewState["VPersName"] = PersName.Text.ToString().Trim(); // RoleName.SelectedValue.ToString().Trim();
        string sSqlSort = " ORDER BY " + ViewState["SortExpression"] + " " + ViewState["SortDirection"];
        Bind(sSqlSort);
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        string sSqlSort = " ORDER BY " + ViewState["SortExpression"] + " " + ViewState["SortDirection"];
        Bind(sSqlSort);
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowIndex != -1)
        {
            HtmlInputCheckBox Chk = (HtmlInputCheckBox)(e.Row.Cells[0].FindControl("OneTicketNo"));
            if (e.Row.RowIndex % 2 != 0)
            {
                e.Row.Attributes.Add("onclick", "GetRowInfo('" + e.Row.Cells[0].Text + "|" + e.Row.Cells[1].Text + "|" + e.Row.Cells[2].Text + "|" + e.Row.Cells[3].Text + "|" + e.Row.Cells[4].Text + "|" + e.Row.Cells[5].Text + "|" + e.Row.Cells[6].Text + "|" + e.Row.Cells[7].Text + "|" + e.Row.Cells[8].Text + "'),tog(this,'AliceBlue')");
            }
            else
            {
                e.Row.Attributes.Add("onclick", "GetRowInfo('" + e.Row.Cells[0].Text + "|" + e.Row.Cells[1].Text + "|" + e.Row.Cells[2].Text + "|" + e.Row.Cells[3].Text + "|" + e.Row.Cells[4].Text + "|" + e.Row.Cells[5].Text + "|" + e.Row.Cells[6].Text + "|" + e.Row.Cells[7].Text + "|" + e.Row.Cells[8].Text + "'),tog(this,'#ffffff')");
            }
            if (ChoiceInfo .Value .IndexOf(e.Row.Cells[1].Text.Trim()) >= 0)
            {
                HtmlInputCheckBox ChkSelected = (HtmlInputCheckBox)(e.Row.Cells[0].FindControl("OneTicketNo"));
                ChkSelected.Checked = true;
            }
        }
    }
    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        if (ViewState["SortDirection"].ToString() == "desc")
        {
            ViewState["SortDirection"] = " ";
        }
        else
        {
            ViewState["SortDirection"] = "desc";
        }

        ViewState["SortExpression"] = e.SortExpression;
        string sSqlSort = " ORDER BY " + ViewState["SortExpression"] + " " + ViewState["SortDirection"];
        Bind(sSqlSort);
    }

    public static string Md5(string str)
    {
        return FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5");
    }

    protected void BtnExport_Click(object sender, EventArgs e)
    {
        string sFileDT = string.Format("{0:yyyyMMddHHmmss}", System.DateTime.Now);
        string sFileName = "UserInfo(" + sFileDT + ").xls";

        ViewState["VLogonID"] = LogonID.Text.ToString().Trim();
        ViewState["VPersName"] = PersName.Text.ToString().Trim(); // RoleName.SelectedValue.ToString().Trim();
        string sSqlSort = " ORDER BY " + ViewState["SortExpression"] + " " + ViewState["SortDirection"];
        DrawExcelFromDataGrid(MYUM.SearchUserInfo(ViewState["VLogonID"].ToString().Trim(), ViewState["VPersName"].ToString().Trim(), sCodeCol, UserID, sSqlSort,""), sFileName);
    }

    /// 从GridView中导出到Excel中去
    private void DrawExcelFromDataGrid(DataView dv, string FileName)
    {
        HttpResponse resp;
        resp = Page.Response;
        resp.ContentEncoding = System.Text.Encoding.Default;
        resp.AppendHeader("Content-Disposition", "attachment;filename=" + FileName);
        string colHeaders = "", ls_item = "";

        string sHeading = "用户帐户|别名|真实姓名|角色名称|用户组|创建者";
        string sColumns = "1|2|13|15|12|10";
        string[] AHeading = sHeading.Split('|');
        int iHeading = AHeading.Length;
        for (int i = 0; i < iHeading - 1; i++)
        {
            colHeaders += AHeading[i] + Convert.ToChar(9);
        }
        colHeaders += AHeading[iHeading - 1] + Convert.ToChar(13);
        resp.Write(colHeaders);


        GridView MyGV = new GridView();
        MyGV.DataSource = dv;
        MyGV.DataBind();

        string[] AColumns = sColumns.Split('|');
        int iColumns = AColumns.Length;

        foreach (GridViewRow row in MyGV.Rows)
        {
            for (int j = 0; j < iColumns - 1; j++)
            {
                int k = Convert.ToInt32(AColumns[j]);
                ls_item = ls_item + backstring(row.Cells[k].Text.ToString().Trim()) + Convert.ToChar(9);
            }
            int p = Convert.ToInt32(AColumns[iColumns - 1]);
            ls_item = ls_item + backstring(row.Cells[p].Text.ToString().Trim()) + Convert.ToChar(13);
            resp.Write(ls_item);
            ls_item = "";
        }
        resp.End();
    }

    protected void BtnDel_Click(object sender, EventArgs e)
    {
        string[] aChoiceID = ChoiceInfo.Value.ToString().Trim().Split ('|');
        int iback = 0;
        for (int i = 0; i < aChoiceID.Length - 1; i++)
        {
            int iback2 = MyUm.IFDelUserInfo (aChoiceID[i], "");
           iback += iback2;
        }
        if (iback >0)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('删除成功!');</script>");
            string sSqlSort = " ORDER BY " + ViewState["SortExpression"] + " " + ViewState["SortDirection"];
            Bind( sSqlSort);
        }
        else
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('删除失败!');</script>");
        }
    }
    protected void BtnDispDel_Click(object sender, EventArgs e)
    {
        ViewState["OpType"] = "2";
        string sSqlSort = " ORDER BY " + ViewState["SortExpression"] + " " + ViewState["SortDirection"];
        Bind(sSqlSort);
    }
    protected void BtnHideDel_Click(object sender, EventArgs e)
    {
        ViewState["OpType"] = "0";
        string sSqlSort = " ORDER BY " + ViewState["SortExpression"] + " " + ViewState["SortDirection"];
        Bind(sSqlSort);
    }
    protected void BtnRefresh_Click(object sender, EventArgs e)
    {
        string sSqlSort = " ORDER BY " + ViewState["SortExpression"] + " " + ViewState["SortDirection"];
        Bind(sSqlSort);
    }
    protected void BtnResume_Click(object sender, EventArgs e)
    {
        string sResumeCode = HUserCode.Value.ToString().Trim();
        int iback = MyUm .IFDelUserInfo (sResumeCode, "1");
        if (iback == 1)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('恢复成功!');</script>");
            ViewState["OpType"] = "2";
            string sSqlSort = " ORDER BY " + ViewState["SortExpression"] + " " + ViewState["SortDirection"];
            Bind(sSqlSort);
        }
        else
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('恢复失败!');</script>");
        }
    }

    public string ListState(string str1)
    {
        if (str1 == "1")
            return "是";
        else
            return "<font color=red>否</font>";
    }
    protected void BtnOriPsd_Click(object sender, EventArgs e)
    {
        string sPassword = Md5("111111"); //用户初始化密码

        string sUserID = HUserCode.Value.ToString().Trim();//用户ID
        int iback = MyUm .OriUserPsd (sUserID, sPassword);
        if (iback == 1)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('还原成功!');</script>");
        }
        else
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('还原失败!');</script>");
        }
    }

    protected string backstring(string stmp)
    {
        if (stmp == "&nbsp;")
        {
            stmp = "";
        }
        return stmp;
    }
}
